class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        int n=nums.size();
        unordered_map<int,int>hash;
        for(int left=0,right=0;right<n;right++)
        {
            int tmp=nums[right];
            hash[tmp]++;
            while(hash[tmp]>1)
            {
                if(right-left<=k)return true;  
                hash[nums[left++]]--;
            }
        }
        return false; 
    }
};
